<?php

namespace app\api\controller\user;

use think\Controller;

class Base extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel=model('User.User');
    }

    public function register()
    {
        //判断是否为post请求
        if($this->request->isPost())
        {
            $params = $this->request->param();

            $password = $this->request->param('password', '');

            if(empty($password))
            {
                $this->error('密码不能为空');
                exit;
            }

            //生成密码盐
            $salt = randstr(10);

            //将密码和密码盐md5加密
            $password = md5(trim($password).$salt);

            //获取系统配置表里面的赠送积分
            $RegisterPoint = model('Config')->where(['key' => 'RegisterPoint'])->value('value');

            //判断一下注册积分 是不是一个整数
            $RegisterPoint = is_numeric($RegisterPoint) ? abs($RegisterPoint) : 0;

            //组装数据
            $data = [
                'email'=>$params['email'],
                'password'=>$password,
                'salt'=>$salt,
                'nickname'=>$params['email'],
                'point'=>$RegisterPoint,
                'vip'=>0,
                'auth'=>0
            ];

            //开始插入数据库  
            $result = $this->UserModel->validate('common/User/User')->save($data);

            if($result === FALSE)
            {
                //注册失败
                // getError 返回操作的结果
                $this->error($this->UserModel->getError());
                exit;
            }else
            {
                //注册成功，跳转到登录界面
                $this->success('注册成功');
                exit;
            }
        }
    }

   // 登录
   public function login()
   {
       if($this->request->isPost())
       {
           $email = $this->request->param('email', '');
           $password = $this->request->param('password', '');

           //查询 toArray 转换为数组
           $User = $this->UserModel->where(['email'=>$email])->find();

           //当没有找到用户的情况下
           if(!$User)
           {
               $this->error('用户不存在，请重新输入');
               exit;
           }


           //判断密码
           $salt = $User['salt'];

           //要让密码加密后做对比
           $password = md5(trim($password).$salt);

           if($password != $User['password'])
           {
               $this->error('密码输入有误，请重新输入');
               exit;
           }

           //设置一个保存cookie信息
           $data = [
               'id'=>$User['id'],
               'email'=>$User['email'],
               'nickname'=>$User['nickname'],
               'avatar'=>$User['avatar_cdn'],
               'sex'=>$User['sex'],
               'province'=>$User['province'],
               'city'=>$User['city'],
               'district'=>$User['district'],
               'point'=>$User['point'],
               'vip'=>$User['vip'],
               'auth'=>$User['auth'],
               'cover'=>$User['cover_cdn'],
               'region'=>$User['region'],
           ];

           //跳转界面
           $this->success('登录成功', '/user/base/index', $data);
           exit;
       }
   }

    public function check(){
        // 判断是否是post请求
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);

            // 查询这个用户是否存在
            $user=$this->UserModel->find($userid);

            if($user){
                // 设置一个保存cookie信息
                $data = [
                    'id'=>$user['id'],
                    'email'=>$user['email'],
                    'nickname'=>$user['nickname'],
                    'avatar'=>$user['avatar_cdn'],
                    'sex'=>$user['sex'],
                    'province'=>$user['province'],
                    'city'=>$user['city'],
                    'district'=>$user['district'],
                    'point'=>$user['point'],
                    'vip'=>$user['vip'],
                    'auth'=>$user['auth'],
                    'cover'=>$user['cover_cdn'],
                    'region'=>$user['region'],
                ];
                $this->success('登录成功',null,$data);
                exit;
            }else{
                $this->error('用户不存在');
                exit;
            }
        }
    }

    public function email(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);

            // 判断用户是否存在
            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            // 判断是否认证过
            if($user['auth'] == 1){
                $this->error('您已经通认证，无须重复认证');
                exit;
            }

            if(empty($user['email'])){
                $this->error('邮箱不能为空');
                exit;
            }

            // 发送邮箱验证
            // 将邮箱进行md5加密
            $email=md5($user['email']);

            // 组装信息
            $msg="<a href='http://www.ask.com/home/index/email?email=$email'>点击免费邮箱验证</a>";

            // 调用公共函数库里面邮箱发送方法
            $result=send($user['email'],$msg);

            // 返回的结果
            $success=[
                'result'=>false,
                'msg'=>''
            ];

            if($result){
                $success['result']=true;
                $success['msg']='发送邮件成功，请注意查收';
            }else{
                $success['result']=false;
                $success['msg']='邮件发送失败，请重新发送';
            }

            return json($success);
        }
    }

    public function profile(){
        if($this->request->isPost()){
            // 获取所有的参数
            $params=$this->request->param();

            $userid=$this->request->param('id',0);
            $password=$this->request->param('password','');

            // 先判断这个人是否存在
            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            // 组装数据
            $data=[
                'id'=>$userid,
                'nickname'=>$params['nickname'],
                'email'=>$params['email'],
                'sex'=>$params['sex'],
                'content'=>$params['content'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
            ];

            // 判断是否修改了邮箱
            if($user['email'] != $params['email']){
                $data['auth']=0;
            }

            // 密码不为空
            if(!empty($password)){
                $salt=$user['salt'];
                $repass=md5($password.$salt);

                // 判断新密码和旧密码是否一致
                if($repass == $user['password']){
                    $this->error('当前密码为旧密码，请重新输入');
                    exit;
                }

                // 生成新的密码盐
                $newsalt=randstr(10);

                // 新密码加密
                $password=md5(trim($password).$newsalt);

                $data['salt']=$newsalt;
                $data['password']=$password;
            }

            // 判断头像
            if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0){
                $result=upload('avatar');

                if($result === FALSE){
                    $this->error('修改头像失败');
                    exit;
                }

                // 追加头像
                $data['avatar']='uploads/'.$result;
            }

            // 封面
            if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0){
                $result=upload('cover');

                if($result === FALSE){
                    $this->error('修改头像失败');
                    exit;
                }

                // 追加封面
                $data['cover']='uploads/'.$result;
            }

            // 更新数据库
            $result=$this->UserModel->isUpdate(true)->save($data);

            if($result === FALSE){
                // 上传上来的图片删除
                @is_file($data['avatar']) && @unlink($data['avatar']);
                @is_file($data['cover']) && @unlink($data['cover']);

                $this->error('修改个人资料失败');
                exit;
            }else{
                // 先判断是否有选择新的图片
                if(@is_file($data['avatar'])){
                    // 修改成功后，将原来的图片删除
                    $avatar = ltrim($user['avatar'],'/');
                    @is_file($avatar) && @unlink($avatar);
                }
                if(@is_file($data['cover'])){
                    // 修改成功后，将原来的图片删除
                    $cover = ltrim($user['cover'],'/');
                    @is_file($cover) && @unlink($cover);
                }

                // 直接查询最新的结果再返回
                $UserData=$this->UserModel->find($userid);

                $data=[
                    'id'=>$UserData['id'],
                    'email'=>$UserData['email'],
                    'nickname'=>$UserData['nickname'],
                    'avatar'=>$UserData['avatar_cdn'],
                    'sex'=>$UserData['sex'],
                    'province'=>$UserData['province'],
                    'city'=>$UserData['city'],
                    'district'=>$UserData['district'],
                    'createtime'=>$UserData['createtime'],
                    'content'=>$UserData['content'],
                    'point'=>$UserData['point'],
                    'vip'=>$UserData['vip'],
                    'auth'=>$UserData['auth'],
                    'cover'=>$UserData['cover_cdn'],
                    'region'=>$UserData['region'],

                ];
                $this->success('修改资料成功','/user/base/index',$data);
                exit;
            }
        }
    }
}
